﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace RFID_EX_WineSecurity.Core {
    class RFIDReader {
        public static readonly byte REQUES_MODE_ALL = 0x52;
        public static readonly byte REQUES_MODE_ALL_ACTIVATE = 0x26;

        private static short icDev = 0x0000;
        private static int baudRate = 19200;

        public static int BaudRate { get => baudRate; set => baudRate = value; }
        public static short IcDev { get => icDev; set => icDev = value; }

        [DllImport("kernel32.dll")]
        public static extern void sleep(int dwMilliseconds);
        [DllImport("MasterRDnew.dll")]   //获取动态库版本号
        public static extern int lib_ver(ref uint pVer);
        [DllImport("MasterRDnew.dll")]   //初始化串口: icdev:设备标识符 port: 串口号 baud: 波特率
        public static extern int rf_init_com(short icdev, int port, int baud);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_antenna_sta(short icdev, byte mode);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_init_type(short icdev, byte type);
        [DllImport("MasterRDnew.dll")]    //寻卡
        public static extern int rf_request(short icdev, byte mode, ref ushort pTagType);
        [DllImport("MasterRDnew.dll")]    //防冲突
        public static extern int rf_anticoll(short icdev, byte bcnt, ref byte pSnr, ref byte pRLength);
        [DllImport("MasterRDnew.dll")]    //选卡
        public static extern int rf_select(short icdev, ref byte pSnr, byte srcLen, ref sbyte Size);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_halt(short icdev);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_M1_authentication2(short icdev, byte mode, byte block, ref byte key);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_M1_initval(short icdev, byte adr, Int32 value);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_M1_increment(short icdev, byte adr, Int32 value);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_M1_decrement(short icdev, byte adr, Int32 value);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_M1_readval(short icdev, byte adr, ref Int32 pValue);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_M1_read(short icdev, byte adr, ref byte pData, ref byte pLen);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_M1_write(short icdev, byte adr, ref byte pData);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_ClosePort();

        [DllImport("MasterRDnew.dll")]
        public static extern int rf_light(short icdev, byte color);
        [DllImport("MasterRDnew.dll")]
        public static extern int rf_beep(short icdev, byte msec);
    }
}
